package com.hks360.library.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hks360.library.R;


public class IListDialog extends Dialog {

    public IListDialog(Context context) {
        super(context);
    }

    public IListDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String negativeButtonText;
        private String[] items;
        private OnClickListener negativeButtonClickListener;
        private OnItemClickListener onItemClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog title from resource
         * 
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         * 
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setItems(String[] items, OnItemClickListener listener) {
            this.items = items;
            this.onItemClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public IListDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final IListDialog dialog = new IListDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_list_layout, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.dialog_list_title)).setText(title);
            // set the cancel button
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.dialog_list_cancel)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.dialog_list_cancel))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }

            if (items != null && items.length > 0) {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.dialog_list_item,
                        R.id.dialog_item_text, items);
                ListView listView = (ListView) layout.findViewById(R.id.dialog_listview);
                listView.setAdapter(adapter);
                if (onItemClickListener != null) {
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            onItemClickListener.onItemClick(dialog, parent, view, position, id);
                        }
                    });
                }
            }

            dialog.setContentView(layout);
            return dialog;
        }

    }

    public interface OnItemClickListener {
        void onItemClick(Dialog dialog, AdapterView<?> parent, View view, int position, long id);
    }

}